package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.entity.AddressBook;
import org.example.entity.Business;
import org.example.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * TODO 收货地址
 *
 * @author shaoxuan.
 * @date 2023/12/23 19:11
 */
@RestController
@Api(tags = "用户地址管理")
@RequestMapping("/address")
@Slf4j
public class AddressController {

    @Autowired
    private AddressBookService service;

    /**
     * 根据用户id获取地址
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id获取地址")
    @GetMapping("/getInfo/{userId}")
    public R<List<AddressBook>> getInfoById(@PathVariable("userId") String userId) {
        List<AddressBook> res = service.list(new LambdaQueryWrapper<AddressBook>().
                eq(AddressBook::getUserId, userId));

        return R.success(res);
    }

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "添加地址")
    @PostMapping("/add")
    public R<AddressBook> addAddress(@RequestBody AddressBook addressBook) {
        if (service.save(addressBook)) {
            return R.success(addressBook);
        } else {
            return R.error("添加失败！");
        }
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "修改地址")

    @PutMapping("/update")
    public R<String> update(@RequestBody AddressBook addressBook) {
        if (service.updateById(addressBook)) {
            return R.success("修改成功");
        } else {
            return R.error("修改成功！");
        }
    }

    /**
     * 删除地址
     * @param addressId
     * @return
     */
    @ApiOperation(value = "根据id删除地址")

    @DeleteMapping("/delete/{id}")
    public R<String> deleteById(@PathVariable("id") String addressId) {
        if (service.removeById(addressId)){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }



}
